package datasource;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.math3.distribution.UniformRealDistribution;
import org.junit.Test;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;

import static com.ruoyi.common.utils.ServletUtils.urlDecode;

@Slf4j
public class datasource {
    //均匀分布
    @Test
    public void test(){
        // 定义数据范围
        double min = -20;
        double max = 20;
        Integer num = 10;
        UniformRealDistribution distribution = new UniformRealDistribution(min, max);
        for (int i = 0; i < num; i++) {
            double randomValue = distribution.sample();
        }
    }

    @Test
    public void test01() {
        try {
            String[] command = {"python", "\"D:\\workspace\\count\\ztfb.py\"", "10", "5" ,"5","ztfb.csv" };
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
               log.info(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 创建文件:
     */
    @Test
    public void createFile(){
        String path = getProjectRootPath();
        File file = new File(path+"myFile.txt");
        if(file.exists()){

        }
        try {
            FileUtils.write(file, "Hello, world!", "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(file.getPath());
    }


    public static String getProjectRootPath(){
        String url = null;
        try {
            url = ResourceUtils.getURL("classpath:").getPath();
        } catch (FileNotFoundException e) {
            log.error("获取项目根目录失败:{}",e.getMessage());
        }
        return urlDecode(new File(url).getAbsolutePath()) + File.separator + "static"+ File.separator ;
    }

    public void testDynaic(){
        HashMap<String, Object> fakeChart = new LinkedHashMap<>();
        fakeChart.put("tableName", "users");
        fakeChart.put("columns", Arrays.asList("id", "name", "email"));
        fakeChart.put("data", Arrays.asList(
                Arrays.asList("1", "John", "john@example.com"),
                Arrays.asList("2", "Bob", "bob@example.com")
        ));
    }
}
